﻿CREATE FUNCTION [dbo].[fn_RoutineExists]
(
	@Routine sysname
)
RETURNS BIT
AS
BEGIN
	DECLARE @True  BIT; SET @True  = 1
	DECLARE @False BIT; SET @False = 0
	DECLARE @Object sysname; SET @Object = PARSENAME(@Routine, 1)
	DECLARE @Schema sysname; SET @Schema = ISNULL(PARSENAME(@Routine, 2), 'dbo')
	
	IF EXISTS (
		   SELECT *
		   FROM   sys.objects o
		   WHERE  o.[type] IN ('P', 'IF', 'TF', 'FN')
				  AND o.[name] = @Object
				  AND schema_name(o.schema_id) = @Schema	 
				  	   )
		RETURN @True

	RETURN @False
END